/**
 * file name  : /it/ciano/util/WebPageFetcher.java
 * authors    : John O'Hanley (Canada)
 * created    : mar 25 lug 2006 16:25:43 CEST
 * license    : Creative Commons: Attribution-NonCommercial-ShareAlike 1.0
 *
 * modifications: by Luciano Xumerle
 *
 */

package it.ciano.util;

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;

/**
 * Fetches the HTML content of a web page as a String[].
 * 
 * @author John O'Hanley (Canada)
 * @author Luciano Xumerle (Italy)
 * @version 0.1
 */
public final class WebPageFetcher
{
    // store the URL object
    private URL fURL;

    // accepted protocol
    private static final String fHTTP = "http";

    /**
     * Class constructor.
     * 
     * @param aURL the URL object.
     */
    public WebPageFetcher ( URL aURL )
    {
        if ( !aURL.getProtocol().equals ( fHTTP ) )
            throw new IllegalArgumentException ( "URL is not for HTTP Protocol: " + aURL );
        fURL = aURL;
    }

    /**
     * Class constructor.
     * 
     * @param aUrlName An url String.
     * @throws MalformedURLException 
     */
    public WebPageFetcher ( String aUrlName ) throws MalformedURLException
    {
        this ( new URL ( aUrlName ) );
    }

    /**
     * Fetch the HTML content of the page as simple text array.
     * 
     * @return a String array.
     */
    public String[] getPageContent()
    {
        ArrayList result = new ArrayList();
        BufferedReader reader = null;
        try
        {
            reader = new BufferedReader ( new InputStreamReader ( fURL.openStream() ) );
            String line = null;
            while ( ( line = reader.readLine() ) != null )
            {
                result.add ( line );
            }
        }
        catch ( IOException ex )
        {
            System.err.println ( "Cannot retrieve contents of: " + fURL );
        }
        finally
        {
            shutdown ( reader );
        }
        return ( String[] ) result.toArray ( new String[result.size() ] );
    }

    /**
     * Fetch the HTML headers as simple text array.
     * 
     * @return a String array.
     */
    public String[] getPageHeader()
    {
        ArrayList result = new ArrayList();

        URLConnection connection = null;
        try
        {
            connection = fURL.openConnection();
        }
        catch ( IOException ex )
        {
            System.err.println ( "Cannot open connection to URL: " + fURL );
        }

        //not all headers come in key-value pairs - sometimes the key is
        //null or an empty String
        int headerIdx = 0;
        String headerKey = null;
        String headerValue = null;
        while ( ( headerValue = connection.getHeaderField ( headerIdx ) ) != null )
        {
            headerKey = connection.getHeaderFieldKey ( headerIdx );
            if ( headerKey != null && headerKey.length() >0 )
                headerKey+=" : ";
            else
                headerKey="";
            result.add ( headerKey + headerValue );
            headerIdx++;
        }
        return ( String[] ) result.toArray ( new String[result.size() ] );
    }

    /**
     * Closes the Reader object.
     * 
     * @param aReader the Reader object.
     */
    private void shutdown ( Reader aReader )
    {
        try
        {
            if ( aReader != null ) aReader.close();
        }
        catch ( IOException ex )
        {
            System.err.println ( "Cannot close reader: " + aReader );
        }
    }

} // end class


